package com.example.careycai.notepad;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Addinfo extends AppCompatActivity {
    private MyDatabaseHelper dbHelp;
    private Button bt1,bt2,query;
    private EditText title,place;
    private MultiAutoCompleteTextView content;
    private static final String mystr[]=new String[]{"abc","ajdf","acc","def","hdf","fgflj","gslj","koe","oke","pop"};

    private List<Information_entity> information_entityList=new ArrayList<Information_entity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addinfo);
        ArrayAdapter<String>aa=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,mystr);
        MultiAutoCompleteTextView autoCt=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
        autoCt.setAdapter(aa);
        autoCt.setThreshold(1);
        title=(EditText)findViewById(R.id.title);
        place=(EditText)findViewById(R.id.place);
        content=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView);
        bt1=(Button)findViewById(R.id.cancel);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent=new Intent(Addinfo.this,MainActivity.class);
                startActivity(intent);
            }
        });
        bt2=(Button)findViewById(R.id.save);
        dbHelp=new MyDatabaseHelper(this,"Notepad.db",null,1);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(title.getText().toString().trim().length()!=0&place.getText().toString().trim().length()!=0&content.getText().toString().trim().length()!=0) {

                    SQLiteDatabase db = dbHelp.getWritableDatabase();
                    String dt;
                    Date cal = Calendar.getInstance().getTime();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    dt = format.format(cal);

                    ContentValues values = new ContentValues();
                    //开始组装
                    values.put("title", title.getText().toString());
                    values.put("content", content.getText().toString());
                    values.put("place", place.getText().toString());
                    values.put("time", dt);

                    db.insert("information", null, values);
                    values.clear();

                    Toast.makeText(Addinfo.this, "添加成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Addinfo.this,MainActivity.class);
                    startActivity(intent);

                    Log.d("Addinfo","添加成功");
                    finish();
                }
                else
                {
                    Toast.makeText(Addinfo.this,"输入框不能为空",Toast.LENGTH_SHORT).show();

                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_addinfo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
